package me.lc.hammerheads.sprite;

import me.lc.hammerheads.R;
import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.core.StageManager;
import me.lc.hammerheads.entity.GameStageInfo;
import me.lc.hammerheads.scene.GameScene;
import me.lc.hammerheads.util.ADHelper;
import me.lc.hammerheads.util.AudioHelper;
import me.lc.hammerheads.util.GameUtil;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;

/**
 * 关卡按钮Node
 * 
 * @author Administrator
 * 
 */
public class StageButtonNode extends Node {
	Button button;
	Button button1;
	Button button2;
	Button button3;
	Label label;
	Sprite locker;
	int stageIndex;
	boolean isLocked = false;

	public StageButtonNode(int index) {
		stageIndex = index - 1;
		GameStageInfo info = null;
		if (stageIndex < StageManager.instance.stageInfoLst.size()) {
			info = StageManager.instance.stageInfoLst.get(stageIndex);
		}
		button = Button.make(R.drawable.stagebutton,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		addChild(button);

		// star btn
		button1 = Button.make(R.drawable.stagebutton_star1,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		button1.setVisible(false);
		addChild(button1);
		button2 = Button.make(R.drawable.stagebutton_star2,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		button2.setVisible(false);
		addChild(button2);
		button3 = Button.make(R.drawable.stagebutton_star3,
				R.drawable.stagebutton_press, this, "onButtonClicked");
		button3.setVisible(false);
		addChild(button3);

		label = Label.make(index + "", 17);
		addChild(label);
		locker = Sprite.make(GameSystem.tex_stageLocker);
		addChild(locker);
		locker.setVisible(false);

		if (info != null) {
			if (info.isLocked()) {
				isLocked = true;
				locker.setVisible(true);
				label.setVisible(false);
			} else {
				// 设置star
				setStar(info);
			}
		}

	}

	/**
	 * 设置关卡得分star
	 * 
	 * @param info
	 */
	private void setStar(GameStageInfo info) {
		// if (info.getScore() > 0) {
		// if (info.getScore() >= GameSystem.Star3Score) {
		// // 3星
		// button.setVisible(false);
		// button1.setVisible(false);
		// button2.setVisible(false);
		// button3.setVisible(true);
		// } else if (info.getScore() >= GameSystem.Star2Score) {
		// // 2星
		// button.setVisible(false);
		// button1.setVisible(false);
		// button2.setVisible(true);
		// button3.setVisible(false);
		// } else {
		// // 1星
		// button.setVisible(false);
		// button1.setVisible(true);
		// button2.setVisible(false);
		// button3.setVisible(false);
		// }
		// } else {
		// button.setVisible(true);
		// button1.setVisible(false);
		// button2.setVisible(false);
		// button3.setVisible(false);
		// }

	}

	public void refresh() {
		if (stageIndex < StageManager.instance.stageInfoLst.size()) {
			GameStageInfo info = StageManager.instance.stageInfoLst
					.get(stageIndex);
			if (info.isLocked()) {
				isLocked = true;
				locker.setVisible(true);
				label.setVisible(false);
			} else {
				isLocked = false;
				locker.setVisible(false);
				label.setVisible(true);
				setStar(info);
			}
		} else {
			isLocked = true;
			locker.setVisible(true);
			label.setVisible(false);
		}
	}

	public void onButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				if (!isLocked) {
					AudioHelper.buttonClick();
					StageManager.instance.currentStage = stageIndex;
					GameManager.instance.startStageGame(stageIndex);
					GameUtil.switchSceneFlipX(GameScene.make());
					ADHelper.showAd();
					if (StageManager.instance.enableAudio == 1) {
						AudioHelper.playMusicInGame();
					}
				}
			}
		});
	}
}
